﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using HomeBuyerTools;
using Microsoft.Advertising.Mobile.UI;
using HomeBuyerTools.ViewModels;

namespace FinanceCalculators.Views.CompareLoansByRate
{
    public partial class CompareLoansByRate : PhoneApplicationPage
    {
        private SolidColorBrush adcontrolborder = new SolidColorBrush(Colors.Transparent);
        Custom objCustom = new Custom();
        Settings savadata = new Settings();
        public CompareLoansByRate()
        {
            InitializeComponent();
            LoadPivotThemes();

        }
      
        private void LoadPivotThemes()
        {
            LayoutRoot.Background = Custom.getPivotBackground();
           
        }
        private void loadClosingcostpercettages()
        {
            List<CompareLoanByRate> objcloscosts = new List<CompareLoanByRate>();

            XDocument xdoc = XDocument.Load("DispalyData/ClosingCosts.xml");
            var eleclosingcost = from i in xdoc.Descendants("percentages") select i;
            foreach (var element in eleclosingcost)
            {
                CompareLoanByRate objclo = new CompareLoanByRate();
                objclo.Closincostsper = element.Value;
                objcloscosts.Add(objclo);
            }
            lstpicClosingCosts.ItemsSource = objcloscosts;
        }//dynamic binding

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
              Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];

              if (darkBackgroundVisibility == Visibility.Visible)
              {
                  //Theme is Dark
              }

              else
              {
                  //TxtMortgagePrincipal.Foreground = new SolidColorBrush(Colors.White);
                  //TxtTermsOfMortgage.Foreground = new SolidColorBrush(Colors.White);
                  //TxtStartingInterestRate.Foreground = new SolidColorBrush(Colors.White);
                  TxtMortgagePrincipal.MouseEnter += TxtMortgagePrincipal_MouseEnter;
                  TxtMortgagePrincipal.MouseLeave += TxtMortgagePrincipal_MouseLeave;
                  TxtTermsOfMortgage.MouseEnter += TxtTermsOfMortgage_MouseEnter;
                  TxtTermsOfMortgage.MouseLeave += TxtTermsOfMortgage_MouseLeave;
                  TxtStartingInterestRate.MouseLeave += TxtStartingInterestRate_MouseLeave;
                  TxtStartingInterestRate.MouseEnter += TxtStartingInterestRate_MouseEnter;
                  //Theme is Light
              }
            loadClosingcostpercettages();
            //TxtStartingInterestRate.Text = savadata.loadInterest();
            List<SettingsProperties> prop = savadata.loadInterest();
            foreach (SettingsProperties irate in prop)
            {
                TxtStartingInterestRate.Text = (irate.InterestRate).ToString();
                TxtMortgagePrincipal.Text = (irate.MortgagePrincipal).ToString();
            }
            
           
        }

        void TxtStartingInterestRate_MouseEnter(object sender, MouseEventArgs e)
        {
            TxtStartingInterestRate.Foreground = new SolidColorBrush(Colors.White);
        }

        void TxtTermsOfMortgage_MouseLeave(object sender, MouseEventArgs e)
        {
          TxtTermsOfMortgage.Foreground=new SolidColorBrush(Colors.Black);
        }

        void TxtStartingInterestRate_MouseLeave(object sender, MouseEventArgs e)
        {
            TxtStartingInterestRate.Foreground = new SolidColorBrush(Colors.Black);
        }

        void TxtTermsOfMortgage_MouseEnter(object sender, MouseEventArgs e)
        {
            TxtTermsOfMortgage.Foreground = new SolidColorBrush(Colors.White);
        }

        void TxtMortgagePrincipal_MouseLeave(object sender, MouseEventArgs e)
        {
            TxtMortgagePrincipal.Foreground = new SolidColorBrush(Colors.Black);
        }

        void TxtMortgagePrincipal_MouseEnter(object sender, MouseEventArgs e)
        {
            TxtMortgagePrincipal.Foreground = new SolidColorBrush(Colors.White);
        }

        //private void BtnCalculate_Click(object sender, RoutedEventArgs e)
        //{
        //    NavigationService.Navigate(new Uri("/Views/CompareLoansByRate/Result.xaml", UriKind.Relative));
        //}

        private void ApplicationBarCalculate_Click(object sender, EventArgs e)
        {
            TxtMortgagePrincipal.Focus();
                if (ErrorReport)
                    NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=2&mp=" + TxtMortgagePrincipal.Text + "&tm=" + TxtTermsOfMortgage.Text + "&ir=" + TxtStartingInterestRate.Text + "&cc=" + (lstpicClosingCosts.SelectedItem as CompareLoanByRate).Closincostsper, UriKind.Relative));
                    //no need to give validation 4 list picker
                else
                    MessageBox.Show("Please enter the values", "Request", MessageBoxButton.OKCancel);

        }
        //private bool errorreport;
        public bool ErrorReport
        {
            get
            {
                bool isTextBoxEmpty = false;
                if (TxtStartingInterestRate.Text != "" && TxtTermsOfMortgage.Text != "" && TxtMortgagePrincipal.Text != "")
                    isTextBoxEmpty = true;
                return isTextBoxEmpty;
            }
        }



        private void TxtStartingInterestRate_LostFocus(object sender, RoutedEventArgs e)
        {

            savadata.AddInterest(Convert.ToDouble(TxtStartingInterestRate.Text),0);

        }

        private void TxtMortgagePrincipal_LostFocus(object sender, RoutedEventArgs e)
        {
           
            savadata.AddInterest(0,Convert.ToDouble(TxtMortgagePrincipal.Text));
        }

        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=4&", UriKind.Relative));
        }
    }
}